package com.example2016.cjj.wxplangamedemo.game2;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.example2016.cjj.wxplangamedemo.view.GameSurfaceView;

import java.util.List;

/**
 * Created by CJJ on 2017/3/26.
 *
 * 战斗机类
 */

public class BattlePlane extends Sprite {

    private boolean collide = false;//战斗机是否被击中标识i

    private boolean isSingleBullet=true; //单发子弹标识

    private int doubleBulletstart=0; //双发模式开始的绘制次数

    private int doubleBulletDuration = 140; //双发模式最多绘制的次数

    private int bombCount=0; //炸弹数量


    public BattlePlane(Bitmap bitmap) {
        super(bitmap);
    }

    @Override
    protected void beforeDraw(Canvas canvas, Paint paint, GameSurfaceView gameView) {
        if( !isDestroy() ){
            //确保战斗机在Canvas范围内
            validatePostion(canvas.getWidth(),canvas.getHeight());

            //每隔7帧发射子弹
            if( getFrame()%7 ==0 ){
                fightBullet(gameView);
            }

        }
    }



    @Override
    protected void afterDraw(Canvas canvas, Paint paint, GameSurfaceView gameView) {
        if( isDestroy() ){
            return;
        }


        if( !collide ){
            //判断战斗机与任意的一个敌机有相交，如果有，那么就爆炸，游戏结束。
            List<EnemyPlane> enemyPlanes = gameView.getAliveEnemys();
            for ( EnemyPlane enemyPlane : enemyPlanes ){
                Point point = getCollidePointWithOther(enemyPlane);
                if( point !=null ){
                    collide = true;
                    //战斗机被击中，游戏结束
                    explode(gameView);
                    break;
                }
            }

            //判断是否与双发子弹道具有碰撞,如果有，那就双发模式
            List<BulletAward> bulletAwards = gameView.getAliveBulletAward();
            for( BulletAward bulletAward : bulletAwards){
                Point point = getCollidePointWithOther(bulletAward);
                if( point!=null ){
                    bulletAward.destroy();
                    isSingleBullet = false;
                    doubleBulletstart =0;
                }
            }


            //判断是否与炸弹道具有碰撞,如果有，那就炸弹数加1
            List<BombAward> bombAwards = gameView.getAliveBombAward();
            for( BombAward bombAward : bombAwards){
                Point point = getCollidePointWithOther(bombAward);
                if( point!=null ){
                    bombAward.destroy();
                    bombCount++;

                }
            }

        }


    }


    //战斗机爆炸
    private void explode(GameSurfaceView gameView) {
        //创建爆炸效果
        Bitmap explosionBitmap = gameView.getExplosinBitmap();
        Explosion explosion = new Explosion(explosionBitmap);
        explosion.moveTo(getX(),getY());  //移动到当前战斗机的x，y坐标
        //添加在gameview中
        gameView.addNeededSprite(explosion);
        //销毁
        destroy();
    }


    //确保战斗机在Canvas范围内
    private void validatePostion(int canvasWidth, int canvasHeight) {
        if( getX() < 0 ){
            setX(0);
        }

        if( getY() < 0 ){
            setY(0);
        }

        RectF planeRect = getRectf();
        if( planeRect.right > canvasWidth ){
            setX( canvasWidth-planeRect.width() );
        }

        if( planeRect.bottom > canvasHeight ){
            setY( canvasHeight - planeRect.height() );
        }

    }




    //发射子弹
    private void fightBullet(GameSurfaceView gameView) {
        float x = getX() + getWidth()/2;
        float y = getY() -5;

        if( isSingleBullet ){
            Bitmap bulletBitmap = gameView.getBulletBitmap();
            Bullet bullet = new Bullet(bulletBitmap);
            bullet.moveTo(x,y);
            gameView.addNeededSprite(bullet);
        }else {
            //双发模式
            float offset = getWidth()/4;
            Bitmap bulletBitmap = gameView.getBulletBitmap();
            Bullet bullet = new Bullet(bulletBitmap);
            bullet.moveTo(x-offset,y);
            gameView.addNeededSprite(bullet);

            Bitmap bulletBitmap2 = gameView.getBulletBitmap();
            Bullet bullet2 = new Bullet(bulletBitmap);
            bullet2.moveTo(x+offset,y);
            gameView.addNeededSprite(bullet2);

            doubleBulletstart ++;
            if( doubleBulletstart >= doubleBulletDuration){
                isSingleBullet = true;
                doubleBulletstart = 0;
            }
        }

    }


    //使用炸弹道具
    public void useBomb(GameSurfaceView gameView){
        if( collide || isDestroy()){
            return;
        }

        if( bombCount > 0){
            //销毁所有处于活动状态的敌机
            List<EnemyPlane> enemyPlanes = gameView.getAliveEnemys();
            for ( EnemyPlane enemyPlane : enemyPlanes ){
                enemyPlane.explode(gameView);
            }

            bombCount --;
        }

    }




    //获取炸弹数
    public int  getBombCount(){
        return bombCount;
    }

}
